package view.ventanas_principales.panels;

import java.awt.Color;
import java.awt.Component;
import java.util.Calendar;

import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

import model.Estado_Evento;

public class FormatoCeldaEvento extends DefaultTableCellRenderer {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int columna_patron;
	private Calendar calendar = Calendar.getInstance();

	// private Estado_Evento estado;

	public FormatoCeldaEvento(int Colpatron) {
		this.columna_patron = Colpatron;
		// this.estado = estado;
	}

	@Override
	public Component getTableCellRendererComponent(JTable table, Object value, boolean selected, boolean focused, int row, int column) {
		setBackground(Color.white);// color de fondo
		table.setForeground(Color.black);// color de texto
		// Si la celda corresponde a una fila con estado FALSE, se cambia el color de fondo a rojo

		Object objectRow = table.getValueAt(row, columna_patron);
		if (objectRow instanceof Calendar) {
			Calendar calendarRow = (Calendar) objectRow;

			int rowColor = compareCaledars(calendarRow);
			Color color;
			if (rowColor == 1) {
				color = Color.GREEN;
			} else if (rowColor == 2) {
				color = Color.YELLOW;
			} else {
				color = Color.RED;
			}

			setBackground(color);
		}

		super.getTableCellRendererComponent(table, value, selected, focused, row, column);
		return this;
	}

	private int compareCaledars(Calendar calendarRow) {
		// 3 ROJO
		// 2 AMARILLO
		// 1 VERDE
		if (this.calendar.get(Calendar.YEAR) >= calendarRow.get(Calendar.YEAR)) {
			if (this.calendar.get(Calendar.MONTH) >= calendarRow.get(Calendar.MONTH)) {
				if (this.calendar.get(Calendar.DAY_OF_MONTH) == calendarRow.get(Calendar.DAY_OF_MONTH)) {
					return 2;
				} else if (this.calendar.get(Calendar.DAY_OF_MONTH) < calendarRow.get(Calendar.DAY_OF_MONTH)) {
					return 1;
				}
			}
			return 3;
		}
		return 3;
	}
}